package com.dev.calculator;

import java.util.List;
import java.util.Map;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import com.dev.IDev;
import com.interactive.SpeekerManager;

/**
 * @Description 计算器
 * @ClassName calculator
 * @Date 2017年1月23日 下午2:11:46
 * @Author yanghao Copyright (c) All Rights Reserved, 2017.
 */
public class Calculator implements IDev {

	//注册关键词
	protected void setKey(List<String> list) {
		list.add("计算");
		list.add("等于");
		list.add("加");
		list.add("减");
		list.add("乘");
		list.add("除");
	}
	public void appRun(Map<String, Object> parameter) {
		// TODO Auto-generated method stub
		
	}
	
	private static boolean off_on = false;
	
	public static void addNumber(String msg) {
		// TODO Auto-generated method stub
		String playStr = null;
		try {
			if(msg.contains("喂")||msg.contains("能听到吗")){
				playStr = "是的  我能听到";
			}else
			if((msg.contains("开启")||msg.contains("打开"))&&msg.contains("计算器")){
				playStr = "好的  已开启计算器  我现在只会简单的运算";
				off_on = true;
			}else if(off_on&&(msg.contains("+")||msg.contains("-")||msg.contains("×")||msg.contains("÷"))){
				 ScriptEngineManager manager = new ScriptEngineManager();  
			     ScriptEngine engine = manager.getEngineByName("js");  
			     Object result = engine.eval(msg);  
			     playStr = msg +"="+result;
			}else if((msg.contains("关闭")||msg.contains("退出"))&&msg.contains("计算器")){
				playStr = "好的  已关闭计算器  再见";
				off_on = false;
			}
		} catch (Exception e) {
			//e.printStackTrace();
			playStr = "对不起  我还不会";
		}
		if(playStr!=null){
			//System.out.println(playStr);
			SpeekerManager.speek(playStr);
		}
	}
	
	private static String getNum(String str) throws Exception{
		String ss[]=str.split("[^-?\\d+.?\\d*]");
		for(String k:ss){
			if(k.matches("-?\\d+.?\\d*")) return k; 
		}
		throw new Exception();
	}
	
	public static void main(String[] aaa){
		String s="hjkfs>d 1.42<jie是111.1\1.11rjk>0.0<jioiure";
		String ss[]=s.split("[^-?\\d+.?\\d*]");
		for(String k:ss)
		if(k.matches("-?\\d+.?\\d*")) System.out.println(k); 
	}
	@Override
	public void addKey(String key) {
		// TODO Auto-generated method stub
		
	}

}
